/******************************************************************************
Copyright:
2009 Jens Collin (jens.collin@dinomin.com)

This file is part of Envox Decompiler.

Envox Decompiler is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Envox Decompiler is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KitchenOS.  If not, see <http://www.gnu.org/licenses/>.

******************************************************************************/
using System;
using System.Collections;

namespace EnvoxDecompiler
{
	/// <summary>
	/// Settings is set up by the arguments to the application and
	/// contains information about how the program should work.
	/// </summary>
	public sealed class Settings
	{
		private static Settings instance = new Settings();

		// Order of levels here are important;
		// the most important ones goes over the lower ones.
		public enum TRACE_LEVEL
		{
			ERROR = 0,
			INFO,
			DEBUG,
		}

		public string inputFilename;
		public string outputFilename;
		public TRACE_LEVEL traceLevel;
		// Argument string in string-form for display
		public string argsString;
		// Global id number for all enteties.
		public int globalIdSeq;
		// String to insert on all block descriptions.
        public string defaultDescription = "Block created by " + Globals.Instance.PRODUCTNAME + 
            ", " + Globals.Instance.COPYRIGHTTEXT;
		
		// List of all notes
		public ArrayList _notesList = new ArrayList();
		// Error string that will be put into the note on page 1 in ens file.
		private string problemsFound;

		public bool unknownInputsFound = false;
		public bool unknownOutputsFound = false;
		private bool bProblemsFound = false;

		public void AddProblemToInfoNote(string str)
		{
			bProblemsFound = true;
			problemsFound += str;
		}


		public static Settings Instance 
		{
			get 
			{
				return instance;
			}
		}

		private Settings()
		{
		}

		void SetDefaults()
		{
			inputFilename = "";
			outputFilename = "outfile.ens";
			traceLevel = TRACE_LEVEL.INFO;
		}


        public void PrintUsage()
        {
            System.Console.WriteLine("----------- Usage information -----------");
            System.Console.WriteLine("EnvoxDecompiler.exe [parameters]");
            System.Console.WriteLine("");
            System.Console.WriteLine("Required parameters:");
            System.Console.WriteLine("-i [Filename]     Input filename (.seq file)");
            System.Console.WriteLine("");
            System.Console.WriteLine("Optional parameters:");
            System.Console.WriteLine("-o [Filename]     Output filename (.ens file), default 'output.ens'");
            System.Console.WriteLine("-v, -version      Prints version information and quits");
            System.Console.WriteLine("-d [Level]        Debug level: 0 = Errors only, 1=Informational (Default), 2=Debug");
        }

        public void PrintVersion()
        {
            System.Console.WriteLine("Envox Decompiler version " + Globals.Instance.VERSION +
               ", " + Globals.Instance.COPYRIGHTTEXT);
        }


		public bool SetupFromArgs(string[] args)
		{
            if (args.Length == 0)
            {
                PrintUsage();
                return false;
            }
            SetDefaults();
            
			int i=0;
			while (i < args.Length)
			{
				// Input file (seq file)
				if (args[i] == "-i")
				{
					if (i+1 < args.Length)
					{
						inputFilename = args[i+1];
						i++;
					}
					else
					{
						Trace.Error("Missing argument to -i");
						return false;
					}
				}
					// Output filename (ens file)
				else if (args[i] == "-o")
				{
					if (i+1 < args.Length)
					{
						outputFilename = args[i+1];
						i++;
					}
					else
					{
						Trace.Error("Missing argument to -o");
						return false;
					}
				}
				// Version
                else if (args[i] == "-version" || args[i] == "-v" || args[i] == "--version" || args[i] == "--v")
				{
                    PrintVersion();
                    return false;
				}
				// trace level
				else if (args[i] == "-d")
				{
					if (i+1 < args.Length)
					{
						if (args[i+1] == "0")
						{
							traceLevel = TRACE_LEVEL.ERROR;
						}
						else if (args[i+1] == "1")
						{
							traceLevel = TRACE_LEVEL.INFO;
						}
						else if (args[i+1] == "2")
						{
							traceLevel = TRACE_LEVEL.DEBUG;
						}
						else
						{
							Trace.Error("Invalid argument to -d. Can be only 0, 1 or 2.");
						}
						i++;
					}
					else
					{
						Trace.Error("Missing argument to -d");
						return false;
					}
				}

				else
				{
					Trace.Error("Unknown parameter: "+args[i]);
					return false;
				}

				i++;
			}
			
			// Make a nice line to print in note in .Ens file.
			for (int j=0; j < args.Length; j++)
			{
				argsString += args[j] + " ";
			}
			// Don't call AddProblemToInfoNote() here because it will set the
			// bPromblemFound flag. This is not a problem.
            problemsFound += "This script has been reverse engineered by the " +
                Globals.Instance.PRODUCTNAME + ", " + Globals.Instance.COPYRIGHTTEXT + 
                ".\r\n\r\nExecution parameters: \r\n" +
			Settings.Instance.argsString + "\r\n\r\n" +
			"Problems found: \r\n";

			return true;
		}


		public string GetProblemDescriptionString()
		{
			if (!bProblemsFound)
			{
				problemsFound += "none.";
				return problemsFound;
			}
			if (Settings.Instance.unknownOutputsFound)
			{
				Settings.Instance.problemsFound += "* Unknown outputs were found!\r\n";
			}
			if (Settings.Instance.unknownInputsFound)
			{
				Settings.Instance.problemsFound += "* Unknown inputs were found!\r\n";
			}

			problemsFound += "\r\n";
			problemsFound += "Errors above will probably affect the functionality of the script " +
				"so check it manually to be safe. A note has been created for each block with problems.\r\n";
			problemsFound += "\r\n";
			problemsFound += "If possible, submit .seq file for analysis or if that is not possible " +
				"send the output run with parameter:\r\n" +
				"'-d 2'\r\n";
			return problemsFound;
		}


		public void Unimplemented(string functionName)
		{
			Trace.Info("Unimplemented function used: " + functionName);
			AddProblemToInfoNote("A not completely implemented function was used during reversal of script: " + functionName + "\r\n");
		}


	} // Class
} // namespace


